package com.yunxi.service.dubboService.impl.sap2pms;

import com.yunxi.core.enums.DMSEnumsEntity;
import com.yunxi.core.web.ApiResult;
import com.yunxi.finance.pojo.VehiclePriceVO;
import com.yunxi.hubToBaseData.pojo.SapVehiclePriceVo;
import com.yunxi.hubToBaseData.service.Sap2baseDataDubboService;
import com.yunxi.model.finance.VehiclePrice;
import com.yunxi.service.localService.finance.VehiclePriceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Sap2baseDataDubboServiceImpl implements Sap2baseDataDubboService {

    @Autowired
    @Qualifier("vehiclePriceServiceImpl")
    VehiclePriceService vehiclePriceService;

    @Override
    public ApiResult saveVehiclePrice(List<SapVehiclePriceVo> vo) {
        if(vo!=null){
            for(int i=0;i<vo.size();i++){
                SapVehiclePriceVo sap = vo.get(i);


                /**
                 * 检查传入价格生效时间是否与现有有效时间有交集
                 * 如果有则修改 现有价格的失效日期  =传入生效日期-1天
                 * */

                //查询传入生效时间是否在 现有价格有效期区间内
              List<VehiclePrice> vehiclePriceList =  vehiclePriceService.checkPriceValiddate(sap);
              if(vehiclePriceList.size()>0){

                  for (VehiclePrice vehiclePrice: vehiclePriceList ) {
                      //将原价格失效日期设置为传入生效日期-1
                      Date date=null;
                      Calendar c = Calendar.getInstance();
                      try {
                           date = new SimpleDateFormat("yyyy-MM-dd").parse(sap.getKodatab());
                      } catch (ParseException e) {
                          e.printStackTrace();
                      }
                      c.setTime(date);
                      int day = c.get(Calendar.DATE);
                      c.set(Calendar.DATE, day-1);
                      String dayBefore = new SimpleDateFormat("yyyy-MM-dd").format(c.getTime());
                      vehiclePrice.setValiDate(dayBefore);
                      vehiclePriceService.saveOrUpdate(vehiclePrice);
                  }
              }

                VehiclePrice vp = new VehiclePrice();

                vp.setPriceId(sap.getKnumh());
                vp.setPriceTypeCode(sap.getKschl());
                vp.setProductCode(sap.getKunnr());
                vp.setProductCode(sap.getMatnr());
                vp.setCompanyCode(sap.getVkorg());
                vp.setCounrty(sap.getLand1());
                //vp.setBatteryPackCode(sap.getz);
                vp.setFeatureCode(sap.getVarcond());
                vp.setAmount(new BigDecimal(sap.getKbetr()));
                vp.setCurrency(sap.getKonwa());
                vp.setNum_(Integer.parseInt(sap.getKpein()));

                vp.setEffectDate(sap.getKodatab());
                vp.setValiDate(sap.getKodatbi());
                vp.setTime_(sap.getTimestamp());
                vp.setMemo1(sap.getZobl1());
                vp.setMemo2(sap.getZobl2());
                vp.setMemo3(sap.getZobl3());
                vp.setMemo4(sap.getZobl4());
                vp.setMemo5(sap.getZobl5());
                //I 新增  U 更新 D 删除  N 仅告知
                if(sap.getIndicator().equals(DMSEnumsEntity.OperateType.DELETE.getKey())){
                    vp.setIsDel(1);
                }
                vehiclePriceService.saveOrUpdate(vp);
            }
        }

        return null;
    }
}
